#!/usr/bin/python
# Copyright (c) 2013 Roger Light <roger@atchoo.org>
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Distribution License v1.0
# which accompanies this distribution. 
#
# The Eclipse Distribution License is available at 
#   http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
#    Roger Light - initial implementation
# This example shows how you can use the MQTT client in a class.
import sys,time
from threading import Thread
from database import *
from tools import *
from tools import *
import paho.mqtt.client as mqtt

class MyMQTTClass(Thread):
    db=Mydata()
    
    def mqtt_on_connect(self, mqttc, obj, flags, rc):
        #pass
        self._mqttc.subscribe("home/soggiorno/temperatura")
        self._mqttc.subscribe("home/soggiorno/umidita")
        self._mqttc.subscribe("home/tenda/temperatura")
        self._mqttc.subscribe("home/tenda/vento")
        self._mqttc.subscribe("home/cucina/temperatura")
        self._mqttc.subscribe("home/cucina/umidita")
        self._mqttc.subscribe("home/caldaia/temperatura")
        self._mqttc.subscribe("home/caldaia/temperatura/impostata")
        self._mqttc.subscribe("home/caldaia/enel")
        self._mqttc.subscribe("home/studio/temperatura")
        self._mqttc.subscribe("home/studio/umidita")
    
    def mqtt_on_message(self, mqttc, obj, msg):
        #logCritical("topic %s" %msg.topic)
        #logCritical("power1 %s power2 %s" %(self.power1,self.power2))
        if msg.topic=="home/soggiorno/temperatura":
            self.db.update_real_time('soggiorno','temp',float(msg.payload))
                
        elif msg.topic=="home/soggiorno/umidita":
            self.db.update_real_time('soggiorno','umid',float(msg.payload))
            
        elif msg.topic=="home/tenda/temperatura":
            self.db.update_real_time('tenda','temp',float(msg.payload))
            
        elif msg.topic=="home/tenda/vento":
            self.db.update_real_time('tenda','vento',float(msg.payload))
            
        elif msg.topic=="home/cucina/temperatura":
            self.db.update_real_time('cucina','temp',float(msg.payload))
            
        elif msg.topic=="home/cucina/umidita":
            self.db.update_real_time('cucina','umid',float(msg.payload))
            
        elif msg.topic=="home/caldaia/temperatura":
            self.db.update_real_time('caldaia','temp',float(msg.payload))
            
        elif msg.topic=="home/caldaia/enel":
            self.db.update_real_time('caldaia','enel',float(msg.payload))
            
        elif msg.topic=="home/studio/temperatura":
            self.db.update_real_time('studio','temp',float(msg.payload))
        
        elif msg.topic=="home/studio/umidita":
            self.db.update_real_time('studio','umid',float(msg.payload))
        else:
            pass
    def mqtt_on_publish(self, mqttc, obj, mid):
        pass
    
    def mqtt_on_subscribe(self, mqttc, obj, mid, granted_qos):
        pass
    
    def mqtt_on_log(self, mqttc, obj, level, string):
        pass
    
    def run(self):
        #logCritical("name %s" %self.nome)
        #clientid=None
        while True:
            try:
                self._mqttc = mqtt.Client()
                self._mqttc.on_message = self.mqtt_on_message
                self._mqttc.on_connect = self.mqtt_on_connect
                self._mqttc.on_publish = self.mqtt_on_publish
                self._mqttc.on_subscribe = self.mqtt_on_subscribe
                self._mqttc.connect("192.168.1.60", 1883, 60)
                self._mqttc.loop_forever()
            except:
                time.sleep(60)
                
run=MyMQTTClass()
run.start()